#!/bin/sh

prepare_tap() {
  if ! `ip link show tap0 > /dev/null 2>&1`; then
    sudo ip tuntap add dev tap0 mode tap
    sudo ip link set tap0 up
    sudo ifconfig tap0 192.168.17.1
  fi
}

boot_initrd() {
    qemu-system-arm -nographic -machine virt -m 128M -cpu cortex-a15 \
        -kernel arm/run/zImage -append "root=/dev/ram0 rootfstype=ramfs rw rdinit=/linuxrc" \
        -initrd arm/run/initrd.gz \
        -device virtio-9p-device,fsdev=vfs0,mount_tag=vfs_d0 -fsdev local,id=vfs0,path=arm/vfs,security_model=mapped-xattr \
        -device virtio-blk-device,drive=vda -drive file=arm/run/vda.ext4,if=none,id=vda,format=raw \
        -device e1000,netdev=net0,mac=52:52:52:52:52:52 -netdev tap,id=net0,ifname=tap0,script=no,downscript=no
}

boot_vda() {
    qemu-system-arm -nographic -machine virt -m 128M -cpu cortex-a15 \
        -kernel arm/run/zImage -append "root=/dev/vda rootfstype=ext4 rw init=/init" \
        -device virtio-9p-device,fsdev=vfs0,mount_tag=vfs_d0 -fsdev local,id=vfs0,path=arm/vfs,security_model=mapped-xattr \
        -device virtio-blk-device,drive=vda -drive file=arm/run/vda.ext4,if=none,id=vda,format=raw \
        -device e1000,netdev=net0,mac=52:52:52:52:52:52 -netdev tap,id=net0,ifname=tap0,script=no,downscript=no
}

boot_uboot() {
    qemu-system-arm -nographic -machine virt -m 128M -cpu cortex-a15 \
        -bios ./arm/run/u-boot-nodtb.bin \
        -device virtio-9p-device,fsdev=vfs0,mount_tag=vfs_d0 -fsdev local,id=vfs0,path=arm/vfs,security_model=mapped-xattr \
        -device virtio-blk-device,drive=vda -drive file=arm/run/vda.ext4,if=none,id=vda,format=raw \
        -drive if=pflash,index=1,format=raw,readonly=off,file=arm/run/flash.img \
        -device e1000,netdev=net0,mac=52:52:52:52:52:52 -netdev tap,id=net0,ifname=tap0,script=no,downscript=no

    # setenv serverip 192.168.17.1
    # setenv ipaddr 192.168.17.3
    # setenv bootargs "root=/dev/vda rootfstype=ext4 rw init=/init"
    # setenv bootcmd "mtd read nor1 0x40080000 0x120000 0x4E6A08;bootz 0x40080000 - 0x40000000"
    # setenv bootcmd "mtd read nor1 0x40090000 0x120000 0x4E6A08;bootz 0x40090000 - 0x40000000"
    # setenv bootcmd "load virtio 0:0 0x40080000 zImage;bootz 0x40080000 - 0x40000000"
    # setenv bootcmd "load virtio 0:0 0x40090000 zImage;bootz 0x40090000 - 0x40000000"
    # saveenv
    # run bootcmd
}

boot_raw() {
    qemu-system-arm -nographic -machine virt -m 128M -cpu cortex-a15 \
        -drive if=pflash,format=raw,readonly=off,cache=directsync,file=arm/run/flash.img \
        -device virtio-9p-device,fsdev=vfs0,mount_tag=vfs_d0 -fsdev local,id=vfs0,path=arm/vfs,security_model=mapped-xattr \
        -device virtio-blk-device,drive=vda -drive file=arm/run/vda.ext4,if=none,id=vda,format=raw \
        -device e1000,netdev=net0,mac=52:52:52:52:52:52 -netdev tap,id=net0,ifname=tap0,script=no,downscript=no

    # setenv mtdparts nor0:896k(loader),128k(env),128k(fdt),5248k(kernel),-(rootfs)
    # setenv bootargs "root=/dev/vda rootfstype=ext4 rw init=/init"
    # setenv bootcmd "mtd read nor0 0x40080000 0x120000 0x4E6A08;bootz 0x40080000 - 0x40000000"
    # setenv bootcmd "mtd read nor0 0x40090000 0x120000 0x4E6A08;bootz 0x40090000 - 0x40000000"
    # run bootcmd
}

if [ $# -lt 1 ]; then
    echo "error: run mode needed"
    echo 'Usage:' $0 '{ initrd | vhd | uboot | raw }'
    exit 0
fi

prepare_tap

if [ $1 = 'initrd' ]; then
    boot_initrd
elif [ $1 = 'vhd' ]; then
    boot_vda
elif [ $1 = 'uboot' ]; then
    boot_uboot
elif [ $1 = 'raw' ]; then
    boot_raw
else
    echo "error: bad run mode"
    echo 'Usage:' $0 '{ initrd | vhd | uboot | raw }'
fi
